const entity = (table) => {
    let template = `
        import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
        import { CommonEntity } from '@entity/common.entity';
        
        // 参数配置表 -  实体类
        @Entity('${table.tableName}', { comment: '${table.remark}' })
        export class ${table.da}Entity extends CommonEntity {
          // 主键，自增int类型
          @PrimaryGeneratedColumn({
            type: 'int',
            name: 'id',
            comment: '${table.remark}',
          })
          id: number;
        
          ${getTypes(table.sub)}
        }
    `
    return template
}

function getTypes (sub) {
    let template = ``
    const typeObj = {
        varchar: ['string', 'string'],
        int: ['number', 'number'],
        datetime: ['date', 'string'],
    }
    for(let i of sub) {
        if(i['驼峰'] === 'id') continue;
        template += `
          // ${i.comment || i.name}，${i.type}(${i.length || '无上限'})
          @Column({
            type: '${i.type}',${i.type !== 'int' && i.length? `
            length: ${i.length},
            `: ''}name: '${i.name}',
            comment: '${i.comment}',
          })
          ${i['驼峰']}: ${typeObj[i.type][1]};
        `
    }
    return template
}


export default entity
